import os.path
import pandas as pd

import numpy as np

def readzzuNeuralIMU(folder):
    return ReadIMUtxt(folder=folder, device_type='zzuNeural').processing()

def read3birdPosAttIMU(folder):
    return ReadIMUtxt(folder=folder, device_type='3birdPosAtt').processing()

class ReadIMUtxt:
    """
    合并文件夹内imu.txt：1.剔除重复行；2.保存为1个文件
    需要指定文件来自哪种设备类型：'zzuNeural', '3birdPosAtt'
    return:
        程序运行结束后，会在这个文件夹下新建一个progressed_data文件夹，保存合并后的文件
    """
    def __init__(self, folder, device_type='zzuNeural'):
        self.folder = folder
        self.device_type = device_type
        self.line_front = None
        self.sametimestamp = None

    def processing(self):
        # 创建用于保存的文件夹
        if not os.path.exists(self.folder+'/progressed_data'):
            os.makedirs(self.folder+'/progressed_data')
        save_path = self.folder+'/progressed_data'
        merger_imu_file = save_path + '/merger_imu.txt'# 建立空的用于合并的txt文档：merger_imu.txt
        if not os.path.exists(merger_imu_file):
            open(merger_imu_file, 'a').close()

            if self.device_type == '3birdPosAtt':
                imufile_list = [file for file in os.listdir(self.folder) if file.startswith('mpu6050_data_')]  # 获取当前文件夹下所有mpu6050_data_开头的文件
                imufile_list.sort(key=lambda x: int(x.split('_')[2].split('.')[0]))  # 按照数字大小排序
                # 访问文件夹下的imu.txt，逐行处理
                for txt_name in imufile_list:
                    print(f'正在合并{txt_name}')
                    with open(os.path.join(self.folder, txt_name), 'r', encoding='utf-8') as f:
                        line_end = sum(1 for line in f)
                        f.seek(0)
                        for index, line in enumerate(f):
                            line_list = line.split(',')

                            if self.line_front != None:  # 检查是否存在半行
                                line = self.line_front + line
                                self.line_front = None
                                line_list = line.split(',')
                            if index == line_end-1:
                                self.line_front = line if len(line_list) <= 13 else None
                                continue
                            # if line_list[-1].strip() == '':
                            #     print(f'{txt_name}文件中的第{index+1}行缺少时间戳')
                            #     continue
                            with open(merger_imu_file, 'a') as merge_f:
                                merge_f.write(line)

            elif self.device_type == 'zzuNeural':
                imufile_list = [file for file in os.listdir(self.folder) if
                                file.startswith('MPU6050_')]  # 获取当前文件夹下所有gps_data_开头的文件
                imufile_list.sort(key=lambda x: int(x.split('_')[1].split('.')[0]))  # 按照数字大小排序
                # 访问文件夹下的imu.txt，逐行处理
                for txt_name in imufile_list:
                    print(f'正在合并{txt_name}')
                    with open(os.path.join(self.folder, txt_name), 'r', encoding='utf-8') as f:
                        line_end = sum(1 for line in f)
                        f.seek(0)
                        for index, line in enumerate(f):
                            if self.line_front != None:  # 检查是否存在半行
                                line = self.line_front + line
                                self.line_front = None
                            if index == line_end - 1:
                                self.line_front = line if len(line) < 62 else None
                                continue
                            with open(merger_imu_file, 'a') as merge_f:
                                merge_f.write(line)

                # 处理时间戳重复的问题：中间缺失的按照前后的插值补全，这个处理仅出现在课题组这个设备中
                print('正在处理时间戳重复的问题，插值补齐')
                data = []
                with open(merger_imu_file, 'r') as file:
                    for index, line in enumerate(file):
                        line = line.split()
                        data.append(np.array([int(element) for element in line]).reshape(1, -1))
                    data = np.concatenate(data)
                    data = pd.DataFrame(data)
                    duplicated_mask = data.duplicated(keep='first', subset=[6])
                    data.loc[duplicated_mask, :] = np.nan  # 将重复行置空
                    nan_rows = data[data.isnull().all(axis=1)].index
                    print(f'时间戳重复的行索引：{nan_rows}')
                    # print(duplicated_mask)
                    # print(data[1530:1540])
                    # print(data[2970:2980])
                    # print(data[196680:196690])
                    data = data.interpolate(method='linear').astype(int)  # 将置空行插值填充
                    # print(data[1530:1540])
                    # print(data[2970:2980])
                    # print(data[196680:196690])
                    data.to_csv(merger_imu_file, sep=',', index=False, header=False)
                    print('插值补齐完毕')

            print(f'imu.txt文件处理完毕')


def ddmm2dddd(EN):
    return np.floor(float(EN)/100) + (float(EN) % 100)/60

if __name__ == '__main__':
    # folder = 'D:/About_code/FlyNueralProject/original_data/P19/20240507/AM/Attitude data'
    # ReadIMUtxt(folder, device_type='zzuNeural').processing()
    folder = 'C:/Users/HP/Desktop/3birds/1.ALLTEST/20240906/EulerTest/device_2'
    ReadIMUtxt(folder, device_type='3birdPosAtt').processing()

